<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>fastdatanode.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_datasource_fastdatanode.js.html">fastdatanode.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview
<a name="line17"></a> * Efficient implementation of DataNode API.
<a name="line18"></a> *
<a name="line19"></a> * The implementation consists of three concrete classes for modelling
<a name="line20"></a> * DataNodes with different characteristics: FastDataNode,
<a name="line21"></a> * FastPrimitiveDataNode and FastListNode.
<a name="line22"></a> *
<a name="line23"></a> * FastDataNode is for bean-like or map-like objects that consists of
<a name="line24"></a> * key/value mappings and where the primary access pattern is by key.
<a name="line25"></a> *
<a name="line26"></a> * FastPrimitiveDataNode wraps primitives like strings, boolean, and numbers.
<a name="line27"></a> *
<a name="line28"></a> * FastListNode is for array-like data nodes. It also supports key-based
<a name="line29"></a> * lookups if the data nodes have an &quot;id&quot; property or if child nodes are
<a name="line30"></a> * explicitly added by name. It is most efficient if these features are not
<a name="line31"></a> * used.
<a name="line32"></a> *
<a name="line33"></a> * FastDataNodes can be constructed from JSON-like objects via the function
<a name="line34"></a> * goog.ds.FastDataNode.fromJs.
<a name="line35"></a>
<a name="line36"></a> */
<a name="line37"></a>
<a name="line38"></a>goog.provide(&#39;goog.ds.AbstractFastDataNode&#39;);
<a name="line39"></a>goog.provide(&#39;goog.ds.FastDataNode&#39;);
<a name="line40"></a>goog.provide(&#39;goog.ds.FastListNode&#39;);
<a name="line41"></a>goog.provide(&#39;goog.ds.PrimitiveFastDataNode&#39;);
<a name="line42"></a>
<a name="line43"></a>goog.require(&#39;goog.ds.DataManager&#39;);
<a name="line44"></a>goog.require(&#39;goog.ds.EmptyNodeList&#39;);
<a name="line45"></a>goog.require(&#39;goog.string&#39;);
<a name="line46"></a>
<a name="line47"></a>/*
<a name="line48"></a> * Implementation note: In order to reduce the number of objects,
<a name="line49"></a> * FastDataNode stores its key/value mappings directly in the FastDataNode
<a name="line50"></a> * object iself (instead of a separate map). To make this work we have to
<a name="line51"></a> * sure that there are no name clashes with other attribute names used by
<a name="line52"></a> * FastDataNode (like dataName and parent). This is especially difficult in
<a name="line53"></a> * the light of automatic renaming by the JavaScript compiler. For this reason,
<a name="line54"></a> * all internal attributes start with &quot;__&quot; so that they are not renamed
<a name="line55"></a> * by the compiler.
<a name="line56"></a> */
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Creates a new abstract data node.
<a name="line60"></a> * @param {string} dataName Name of the datanode.
<a name="line61"></a> * @param {goog.ds.DataNode=} opt_parent Parent of this data node.
<a name="line62"></a> * @constructor
<a name="line63"></a> * @extends {goog.ds.DataNodeList}
<a name="line64"></a> */
<a name="line65"></a>// TODO(arv): Use interfaces when available.
<a name="line66"></a>goog.ds.AbstractFastDataNode = function(dataName, opt_parent) {
<a name="line67"></a>  if (!dataName) {
<a name="line68"></a>    throw Error(&#39;Cannot create a fast data node without a data name&#39;);
<a name="line69"></a>  }
<a name="line70"></a>  this[&#39;__dataName&#39;] = dataName;
<a name="line71"></a>  this[&#39;__parent&#39;] = opt_parent;
<a name="line72"></a>};
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Return the name of this data node.
<a name="line77"></a> * @return {string} Name of this data noden.
<a name="line78"></a> * @override
<a name="line79"></a> */
<a name="line80"></a>goog.ds.AbstractFastDataNode.prototype.getDataName = function() {
<a name="line81"></a>  return this[&#39;__dataName&#39;];
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Set the name of this data node.
<a name="line87"></a> * @param {string} value Name.
<a name="line88"></a> * @override
<a name="line89"></a> */
<a name="line90"></a>goog.ds.AbstractFastDataNode.prototype.setDataName = function(value) {
<a name="line91"></a>  this[&#39;__dataName&#39;] = value;
<a name="line92"></a>};
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Get the path leading to this data node.
<a name="line97"></a> * @return {string} Data path.
<a name="line98"></a> * @override
<a name="line99"></a> */
<a name="line100"></a>goog.ds.AbstractFastDataNode.prototype.getDataPath = function() {
<a name="line101"></a>  var parentPath;
<a name="line102"></a>  if (this[&#39;__parent&#39;]) {
<a name="line103"></a>    parentPath = this[&#39;__parent&#39;].getDataPath() + goog.ds.STR_PATH_SEPARATOR;
<a name="line104"></a>  } else {
<a name="line105"></a>    parentPath = &#39;&#39;;
<a name="line106"></a>  }
<a name="line107"></a>  return parentPath + this.getDataName();
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * Creates a new fast data node, using the properties of root.
<a name="line114"></a> * @param {Object} root JSON-like object to initialize data node from.
<a name="line115"></a> * @param {string} dataName Name of this data node.
<a name="line116"></a> * @param {goog.ds.DataNode=} opt_parent Parent of this data node.
<a name="line117"></a> * @extends {goog.ds.AbstractFastDataNode}
<a name="line118"></a> * @constructor
<a name="line119"></a> */
<a name="line120"></a>goog.ds.FastDataNode = function(root, dataName, opt_parent) {
<a name="line121"></a>  goog.ds.AbstractFastDataNode.call(this, dataName, opt_parent);
<a name="line122"></a>  this.extendWith_(root);
<a name="line123"></a>};
<a name="line124"></a>goog.inherits(goog.ds.FastDataNode, goog.ds.AbstractFastDataNode);
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Add all attributes of object to this data node.
<a name="line129"></a> * @param {Object} object Object to add attributes from.
<a name="line130"></a> * @protected
<a name="line131"></a> * @suppress {underscore}
<a name="line132"></a> */
<a name="line133"></a>goog.ds.FastDataNode.prototype.extendWith_ = function(object) {
<a name="line134"></a>  for (var key in object) {
<a name="line135"></a>    this[key] = object[key];
<a name="line136"></a>  }
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Creates a new FastDataNode structure initialized from object. This will
<a name="line142"></a> * return an instance of the most suitable sub-class of FastDataNode.
<a name="line143"></a> *
<a name="line144"></a> * You should not modify object after creating a fast data node from it
<a name="line145"></a> * or assume that changing object changes the data node. Doing so results
<a name="line146"></a> * in undefined behaviour.
<a name="line147"></a> *
<a name="line148"></a> * @param {Object|number|boolean|string} object Object to initialize data
<a name="line149"></a> *     node from.
<a name="line150"></a> * @param {string} dataName Name of data node.
<a name="line151"></a> * @param {goog.ds.DataNode=} opt_parent Parent of data node.
<a name="line152"></a> * @return {goog.ds.AbstractFastDataNode} Data node representing object.
<a name="line153"></a> */
<a name="line154"></a>goog.ds.FastDataNode.fromJs = function(object, dataName, opt_parent) {
<a name="line155"></a>  if (goog.isArray(object)) {
<a name="line156"></a>    return new goog.ds.FastListNode(object, dataName, opt_parent);
<a name="line157"></a>  } else if (goog.isObject(object)) {
<a name="line158"></a>    return new goog.ds.FastDataNode(object, dataName, opt_parent);
<a name="line159"></a>  } else {
<a name="line160"></a>    return new goog.ds.PrimitiveFastDataNode(object || !!object,
<a name="line161"></a>                                             dataName,
<a name="line162"></a>                                             opt_parent);
<a name="line163"></a>  }
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Static instance of an empty list.
<a name="line169"></a> * @type {goog.ds.EmptyNodeList}
<a name="line170"></a> * @private
<a name="line171"></a> */
<a name="line172"></a>goog.ds.FastDataNode.emptyList_ = new goog.ds.EmptyNodeList();
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Not supported for normal FastDataNodes.
<a name="line177"></a> * @param {*} value Value to set data node to.
<a name="line178"></a> * @override
<a name="line179"></a> */
<a name="line180"></a>goog.ds.FastDataNode.prototype.set = function(value) {
<a name="line181"></a>  throw &#39;Not implemented yet&#39;;
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/** @override */
<a name="line186"></a>goog.ds.FastDataNode.prototype.getChildNodes = function(opt_selector) {
<a name="line187"></a>  if (!opt_selector || opt_selector == goog.ds.STR_ALL_CHILDREN_SELECTOR) {
<a name="line188"></a>    return this;
<a name="line189"></a>  } else if (opt_selector.indexOf(goog.ds.STR_WILDCARD) == -1) {
<a name="line190"></a>    var child = this.getChildNode(opt_selector);
<a name="line191"></a>    return child ? new goog.ds.FastListNode([child], &#39;&#39;) :
<a name="line192"></a>        new goog.ds.EmptyNodeList();
<a name="line193"></a>  } else {
<a name="line194"></a>    throw Error(&#39;Unsupported selector: &#39; + opt_selector);
<a name="line195"></a>  }
<a name="line196"></a>};
<a name="line197"></a>
<a name="line198"></a>
<a name="line199"></a>/**
<a name="line200"></a> * Makes sure that a named child is wrapped in a data node structure.
<a name="line201"></a> * @param {string} name Name of child to wrap.
<a name="line202"></a> * @private
<a name="line203"></a> */
<a name="line204"></a>goog.ds.FastDataNode.prototype.wrapChild_ = function(name) {
<a name="line205"></a>  var child = this[name];
<a name="line206"></a>  if (child != null &amp;&amp; !child.getDataName) {
<a name="line207"></a>    this[name] = goog.ds.FastDataNode.fromJs(this[name], name, this);
<a name="line208"></a>  }
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Get a child node by name.
<a name="line214"></a> * @param {string} name Name of child node.
<a name="line215"></a> * @param {boolean=} opt_create Whether to create the child if it does not
<a name="line216"></a> * exist.
<a name="line217"></a> * @return {goog.ds.DataNode} Child node.
<a name="line218"></a> * @override
<a name="line219"></a> */
<a name="line220"></a>goog.ds.FastDataNode.prototype.getChildNode = function(name, opt_create) {
<a name="line221"></a>  this.wrapChild_(name);
<a name="line222"></a>  // this[name] always is a data node object, so using &quot;||&quot; is fine.
<a name="line223"></a>  var child = this[name] || null;
<a name="line224"></a>  if (child == null &amp;&amp; opt_create) {
<a name="line225"></a>    child = new goog.ds.FastDataNode({}, name, this);
<a name="line226"></a>    this[name] = child;
<a name="line227"></a>  }
<a name="line228"></a>  return child;
<a name="line229"></a>};
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/**
<a name="line233"></a> * Sets a child node. Creates the child if it does not exist.
<a name="line234"></a> *
<a name="line235"></a> * Calling  this function makes any child nodes previously obtained for name
<a name="line236"></a> * invalid. You should not use these child nodes but instead obtain a new
<a name="line237"></a> * instance by calling getChildNode.
<a name="line238"></a> *
<a name="line239"></a> * @override
<a name="line240"></a> */
<a name="line241"></a>goog.ds.FastDataNode.prototype.setChildNode = function(name, value) {
<a name="line242"></a>  if (value != null) {
<a name="line243"></a>    this[name] = value;
<a name="line244"></a>  } else {
<a name="line245"></a>    delete this[name];
<a name="line246"></a>  }
<a name="line247"></a>  goog.ds.DataManager.getInstance().fireDataChange(this.getDataPath() +
<a name="line248"></a>      goog.ds.STR_PATH_SEPARATOR + name);
<a name="line249"></a>  return null;
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Returns the value of a child node. By using this method you can avoid
<a name="line255"></a> * the need to create PrimitiveFastData nodes.
<a name="line256"></a> * @param {string} name Name of child node.
<a name="line257"></a> * @return {Object} Value of child node.
<a name="line258"></a> * @override
<a name="line259"></a> */
<a name="line260"></a>goog.ds.FastDataNode.prototype.getChildNodeValue = function(name) {
<a name="line261"></a>  var child = this[name];
<a name="line262"></a>  if (child != null) {
<a name="line263"></a>    return (child.getDataName ? child.get() : child);
<a name="line264"></a>  } else {
<a name="line265"></a>    return null;
<a name="line266"></a>  }
<a name="line267"></a>};
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>/**
<a name="line271"></a> * Returns whether this data node is a list. Always returns false for
<a name="line272"></a> * instances of FastDataNode but may return true for subclasses.
<a name="line273"></a> * @return {boolean} Whether this data node is array-like.
<a name="line274"></a> * @override
<a name="line275"></a> */
<a name="line276"></a>goog.ds.FastDataNode.prototype.isList = function() {
<a name="line277"></a>  return false;
<a name="line278"></a>};
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>/**
<a name="line282"></a> * Returns a javascript object representation of this data node. You should
<a name="line283"></a> * not modify the object returned by this function.
<a name="line284"></a> * @return {Object} Javascript object representation of this data node.
<a name="line285"></a> */
<a name="line286"></a>goog.ds.FastDataNode.prototype.getJsObject = function() {
<a name="line287"></a>  var result = {};
<a name="line288"></a>  for (var key in this) {
<a name="line289"></a>    if (!goog.string.startsWith(key, &#39;__&#39;) &amp;&amp; !goog.isFunction(this[key])) {
<a name="line290"></a>      result[key] = (this[key][&#39;__dataName&#39;] ? this[key].getJsObject() :
<a name="line291"></a>          this[key]);
<a name="line292"></a>    }
<a name="line293"></a>  }
<a name="line294"></a>  return result;
<a name="line295"></a>};
<a name="line296"></a>
<a name="line297"></a>
<a name="line298"></a>/**
<a name="line299"></a> * Creates a deep copy of this data node.
<a name="line300"></a> * @return {goog.ds.FastDataNode} Clone of this data node.
<a name="line301"></a> */
<a name="line302"></a>goog.ds.FastDataNode.prototype.clone = function() {
<a name="line303"></a>  return /** @type {goog.ds.FastDataNode} */(goog.ds.FastDataNode.fromJs(
<a name="line304"></a>      this.getJsObject(), this.getDataName()));
<a name="line305"></a>};
<a name="line306"></a>
<a name="line307"></a>
<a name="line308"></a>/*
<a name="line309"></a> * Implementation of goog.ds.DataNodeList for FastDataNode.
<a name="line310"></a> */
<a name="line311"></a>
<a name="line312"></a>
<a name="line313"></a>/**
<a name="line314"></a> * Adds a child to this data node.
<a name="line315"></a> * @param {goog.ds.DataNode} value Child node to add.
<a name="line316"></a> * @override
<a name="line317"></a> */
<a name="line318"></a>goog.ds.FastDataNode.prototype.add = function(value) {
<a name="line319"></a>  this.setChildNode(value.getDataName(), value);
<a name="line320"></a>};
<a name="line321"></a>
<a name="line322"></a>
<a name="line323"></a>/**
<a name="line324"></a> * Gets the value of this data node (if called without opt_key) or
<a name="line325"></a> * gets a child node (if called with opt_key).
<a name="line326"></a> * @param {string=} opt_key Name of child node.
<a name="line327"></a> * @return {*} This data node or a child node.
<a name="line328"></a> * @override
<a name="line329"></a> */
<a name="line330"></a>goog.ds.FastDataNode.prototype.get = function(opt_key) {
<a name="line331"></a>  if (!goog.isDef(opt_key)) {
<a name="line332"></a>    // if there is no key, DataNode#get was called
<a name="line333"></a>    return this;
<a name="line334"></a>  } else {
<a name="line335"></a>    return this.getChildNode(opt_key);
<a name="line336"></a>  }
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/**
<a name="line341"></a> * Gets a child node by index. This method has a complexity of O(n) where
<a name="line342"></a> * n is the number of children. If you need a faster implementation of this
<a name="line343"></a> * method, you should use goog.ds.FastListNode.
<a name="line344"></a> * @param {number} index Index of child node (starting from 0).
<a name="line345"></a> * @return {goog.ds.DataNode} Child node at specified index.
<a name="line346"></a> * @override
<a name="line347"></a> */
<a name="line348"></a>goog.ds.FastDataNode.prototype.getByIndex = function(index) {
<a name="line349"></a>  var i = 0;
<a name="line350"></a>  for (var key in this) {
<a name="line351"></a>    if (!goog.string.startsWith(key, &#39;__&#39;) &amp;&amp; !goog.isFunction(this[key])) {
<a name="line352"></a>      if (i == index) {
<a name="line353"></a>        this.wrapChild_(key);
<a name="line354"></a>        return this[key];
<a name="line355"></a>      }
<a name="line356"></a>      ++i;
<a name="line357"></a>    }
<a name="line358"></a>  }
<a name="line359"></a>  return null;
<a name="line360"></a>};
<a name="line361"></a>
<a name="line362"></a>
<a name="line363"></a>/**
<a name="line364"></a> * Gets the number of child nodes. This method has a complexity of O(n) where
<a name="line365"></a> * n is the number of children. If you need a faster implementation of this
<a name="line366"></a> * method, you should use goog.ds.FastListNode.
<a name="line367"></a> * @return {number} Number of child nodes.
<a name="line368"></a> * @override
<a name="line369"></a> */
<a name="line370"></a>goog.ds.FastDataNode.prototype.getCount = function() {
<a name="line371"></a>  var count = 0;
<a name="line372"></a>  for (var key in this) {
<a name="line373"></a>    if (!goog.string.startsWith(key, &#39;__&#39;) &amp;&amp; !goog.isFunction(this[key])) {
<a name="line374"></a>      ++count;
<a name="line375"></a>    }
<a name="line376"></a>  }
<a name="line377"></a>  // maybe cache this?
<a name="line378"></a>  return count;
<a name="line379"></a>};
<a name="line380"></a>
<a name="line381"></a>
<a name="line382"></a>/**
<a name="line383"></a> * Sets a child node.
<a name="line384"></a> * @param {string} name Name of child node.
<a name="line385"></a> * @param {Object} value Value of child node.
<a name="line386"></a> * @override
<a name="line387"></a> */
<a name="line388"></a>goog.ds.FastDataNode.prototype.setNode = function(name, value) {
<a name="line389"></a>  this.setChildNode(name, value);
<a name="line390"></a>};
<a name="line391"></a>
<a name="line392"></a>
<a name="line393"></a>/**
<a name="line394"></a> * Removes a child node.
<a name="line395"></a> * @override
<a name="line396"></a> */
<a name="line397"></a>goog.ds.FastDataNode.prototype.removeNode = function(name) {
<a name="line398"></a>  delete this[name];
<a name="line399"></a>  return false;
<a name="line400"></a>};
<a name="line401"></a>
<a name="line402"></a>
<a name="line403"></a>
<a name="line404"></a>/**
<a name="line405"></a> * Creates a new data node wrapping a primitive value.
<a name="line406"></a> * @param {number|boolean|string} value Value the value to wrap.
<a name="line407"></a> * @param {string} dataName name Name of this data node.
<a name="line408"></a> * @param {goog.ds.DataNode=} opt_parent Parent of this data node.
<a name="line409"></a> * @extends {goog.ds.AbstractFastDataNode}
<a name="line410"></a> * @constructor
<a name="line411"></a> */
<a name="line412"></a>goog.ds.PrimitiveFastDataNode = function(value, dataName, opt_parent) {
<a name="line413"></a>  this.value_ = value;
<a name="line414"></a>  goog.ds.AbstractFastDataNode.call(this, dataName, opt_parent);
<a name="line415"></a>};
<a name="line416"></a>goog.inherits(goog.ds.PrimitiveFastDataNode, goog.ds.AbstractFastDataNode);
<a name="line417"></a>
<a name="line418"></a>
<a name="line419"></a>/**
<a name="line420"></a> * Returns the value of this data node.
<a name="line421"></a> * @return {(boolean|number|string)} Value of this data node.
<a name="line422"></a> * @override
<a name="line423"></a> */
<a name="line424"></a>goog.ds.PrimitiveFastDataNode.prototype.get = function() {
<a name="line425"></a>  return this.value_;
<a name="line426"></a>};
<a name="line427"></a>
<a name="line428"></a>
<a name="line429"></a>/**
<a name="line430"></a> * Sets this data node to a new value.
<a name="line431"></a> * @param {*} value Value to set data node to.
<a name="line432"></a> * @override
<a name="line433"></a> */
<a name="line434"></a>goog.ds.PrimitiveFastDataNode.prototype.set = function(value) {
<a name="line435"></a>  if (goog.isArray(value) || goog.isObject(value)) {
<a name="line436"></a>    throw Error(&#39;can only set PrimitiveFastDataNode to primitive values&#39;);
<a name="line437"></a>  }
<a name="line438"></a>  this.value_ = value;
<a name="line439"></a>  goog.ds.DataManager.getInstance().fireDataChange(this.getDataPath());
<a name="line440"></a>};
<a name="line441"></a>
<a name="line442"></a>
<a name="line443"></a>/**
<a name="line444"></a> * Returns child nodes of this data node. Always returns an unmodifiable,
<a name="line445"></a> * empty list.
<a name="line446"></a> * @return {goog.ds.DataNodeList} (Empty) list of child nodes.
<a name="line447"></a> * @override
<a name="line448"></a> */
<a name="line449"></a>goog.ds.PrimitiveFastDataNode.prototype.getChildNodes = function() {
<a name="line450"></a>  return goog.ds.FastDataNode.emptyList_;
<a name="line451"></a>};
<a name="line452"></a>
<a name="line453"></a>
<a name="line454"></a>/**
<a name="line455"></a> * Get a child node by name. Always returns null.
<a name="line456"></a> * @param {string} name Name of child node.
<a name="line457"></a> * @return {goog.ds.DataNode} Child node.
<a name="line458"></a> * @override
<a name="line459"></a> */
<a name="line460"></a>goog.ds.PrimitiveFastDataNode.prototype.getChildNode = function(name) {
<a name="line461"></a>  return null;
<a name="line462"></a>};
<a name="line463"></a>
<a name="line464"></a>
<a name="line465"></a>/**
<a name="line466"></a> * Returns the value of a child node. Always returns null.
<a name="line467"></a> * @param {string} name Name of child node.
<a name="line468"></a> * @return {Object} Value of child node.
<a name="line469"></a> * @override
<a name="line470"></a> */
<a name="line471"></a>goog.ds.PrimitiveFastDataNode.prototype.getChildNodeValue = function(name) {
<a name="line472"></a>  return null;
<a name="line473"></a>};
<a name="line474"></a>
<a name="line475"></a>
<a name="line476"></a>/**
<a name="line477"></a> * Not supported by primitive data nodes.
<a name="line478"></a> * @param {string} name Name of child node.
<a name="line479"></a> * @param {Object} value Value of child node.
<a name="line480"></a> * @override
<a name="line481"></a> */
<a name="line482"></a>goog.ds.PrimitiveFastDataNode.prototype.setChildNode =
<a name="line483"></a>    function(name, value) {
<a name="line484"></a>  throw Error(&#39;Cannot set a child node for a PrimitiveFastDataNode&#39;);
<a name="line485"></a>};
<a name="line486"></a>
<a name="line487"></a>
<a name="line488"></a>/**
<a name="line489"></a> * Returns whether this data node is a list. Always returns false for
<a name="line490"></a> * instances of PrimitiveFastDataNode.
<a name="line491"></a> * @return {boolean} Whether this data node is array-like.
<a name="line492"></a> * @override
<a name="line493"></a> */
<a name="line494"></a>goog.ds.PrimitiveFastDataNode.prototype.isList = function() {
<a name="line495"></a>  return false;
<a name="line496"></a>};
<a name="line497"></a>
<a name="line498"></a>
<a name="line499"></a>/**
<a name="line500"></a> * Returns a javascript object representation of this data node. You should
<a name="line501"></a> * not modify the object returned by this function.
<a name="line502"></a> * @return {*} Javascript object representation of this data node.
<a name="line503"></a> */
<a name="line504"></a>goog.ds.PrimitiveFastDataNode.prototype.getJsObject = function() {
<a name="line505"></a>  return this.value_;
<a name="line506"></a>};
<a name="line507"></a>
<a name="line508"></a>
<a name="line509"></a>/**
<a name="line510"></a> * Creates a new list node from an array.
<a name="line511"></a> * @param {Array} values values hold by this list node.
<a name="line512"></a> * @param {string} dataName name of this node.
<a name="line513"></a> * @param {goog.ds.DataNode=} opt_parent parent of this node.
<a name="line514"></a> * @extends {goog.ds.AbstractFastDataNode}
<a name="line515"></a> * @constructor
<a name="line516"></a> */
<a name="line517"></a>// TODO(arv): Use interfaces when available.  This implements DataNodeList
<a name="line518"></a>// as well.
<a name="line519"></a>goog.ds.FastListNode = function(values, dataName, opt_parent) {
<a name="line520"></a>  this.values_ = [];
<a name="line521"></a>  for (var i = 0; i &lt; values.length; ++i) {
<a name="line522"></a>    var name = values[i].id || (&#39;[&#39; + i + &#39;]&#39;);
<a name="line523"></a>    this.values_.push(goog.ds.FastDataNode.fromJs(values[i], name, this));
<a name="line524"></a>    if (values[i].id) {
<a name="line525"></a>      if (!this.map_) {
<a name="line526"></a>        this.map_ = {};
<a name="line527"></a>      }
<a name="line528"></a>      this.map_[values[i].id] = i;
<a name="line529"></a>    }
<a name="line530"></a>  }
<a name="line531"></a>  goog.ds.AbstractFastDataNode.call(this, dataName, opt_parent);
<a name="line532"></a>};
<a name="line533"></a>goog.inherits(goog.ds.FastListNode, goog.ds.AbstractFastDataNode);
<a name="line534"></a>
<a name="line535"></a>
<a name="line536"></a>/**
<a name="line537"></a> * Not supported for FastListNodes.
<a name="line538"></a> * @param {*} value Value to set data node to.
<a name="line539"></a> * @override
<a name="line540"></a> */
<a name="line541"></a>goog.ds.FastListNode.prototype.set = function(value) {
<a name="line542"></a>  throw Error(&#39;Cannot set a FastListNode to a new value&#39;);
<a name="line543"></a>};
<a name="line544"></a>
<a name="line545"></a>
<a name="line546"></a>/**
<a name="line547"></a> * Returns child nodes of this data node. Currently, only supports
<a name="line548"></a> * returning all children.
<a name="line549"></a> * @return {goog.ds.DataNodeList} List of child nodes.
<a name="line550"></a> * @override
<a name="line551"></a> */
<a name="line552"></a>goog.ds.FastListNode.prototype.getChildNodes = function() {
<a name="line553"></a>  return this;
<a name="line554"></a>};
<a name="line555"></a>
<a name="line556"></a>
<a name="line557"></a>/**
<a name="line558"></a> * Get a child node by name.
<a name="line559"></a> * @param {string} key Name of child node.
<a name="line560"></a> * @param {boolean=} opt_create Whether to create the child if it does not
<a name="line561"></a> * exist.
<a name="line562"></a> * @return {goog.ds.DataNode} Child node.
<a name="line563"></a> * @override
<a name="line564"></a> */
<a name="line565"></a>goog.ds.FastListNode.prototype.getChildNode = function(key, opt_create) {
<a name="line566"></a>  var index = this.getKeyAsNumber_(key);
<a name="line567"></a>  if (index == null &amp;&amp; this.map_) {
<a name="line568"></a>    index = this.map_[key];
<a name="line569"></a>  }
<a name="line570"></a>  if (index != null &amp;&amp; this.values_[index]) {
<a name="line571"></a>    return this.values_[index];
<a name="line572"></a>  } else if (opt_create) {
<a name="line573"></a>    this.setChildNode(key, {});
<a name="line574"></a>    return this.getChildNode(key);
<a name="line575"></a>  } else {
<a name="line576"></a>    return null;
<a name="line577"></a>  }
<a name="line578"></a>};
<a name="line579"></a>
<a name="line580"></a>
<a name="line581"></a>/**
<a name="line582"></a> * Returns the value of a child node.
<a name="line583"></a> * @param {string} key Name of child node.
<a name="line584"></a> * @return {*} Value of child node.
<a name="line585"></a> * @override
<a name="line586"></a> */
<a name="line587"></a>goog.ds.FastListNode.prototype.getChildNodeValue = function(key) {
<a name="line588"></a>  var child = this.getChildNode(key);
<a name="line589"></a>  return (child ? child.get() : null);
<a name="line590"></a>};
<a name="line591"></a>
<a name="line592"></a>
<a name="line593"></a>/**
<a name="line594"></a> * Tries to interpret key as a numeric index enclosed by square brakcets.
<a name="line595"></a> * @param {string} key Key that should be interpreted as a number.
<a name="line596"></a> * @return {?number} Numeric index or null if key is not of the form
<a name="line597"></a> *  described above.
<a name="line598"></a> * @private
<a name="line599"></a> */
<a name="line600"></a>goog.ds.FastListNode.prototype.getKeyAsNumber_ = function(key) {
<a name="line601"></a>  if (key.charAt(0) == &#39;[&#39; &amp;&amp; key.charAt(key.length - 1) == &#39;]&#39;) {
<a name="line602"></a>    return Number(key.substring(1, key.length - 1));
<a name="line603"></a>  } else {
<a name="line604"></a>    return null;
<a name="line605"></a>  }
<a name="line606"></a>};
<a name="line607"></a>
<a name="line608"></a>
<a name="line609"></a>/**
<a name="line610"></a> * Sets a child node. Creates the child if it does not exist. To set
<a name="line611"></a> * children at a certain index, use a key of the form &#39;[index]&#39;. Note, that
<a name="line612"></a> * you can only set values at existing numeric indices. To add a new node
<a name="line613"></a> * to this list, you have to use the add method.
<a name="line614"></a> *
<a name="line615"></a> * Calling  this function makes any child nodes previously obtained for name
<a name="line616"></a> * invalid. You should not use these child nodes but instead obtain a new
<a name="line617"></a> * instance by calling getChildNode.
<a name="line618"></a> *
<a name="line619"></a> * @override
<a name="line620"></a> */
<a name="line621"></a>goog.ds.FastListNode.prototype.setChildNode = function(key, value) {
<a name="line622"></a>  var count = this.values_.length;
<a name="line623"></a>  if (value != null) {
<a name="line624"></a>    if (!value.getDataName) {
<a name="line625"></a>      value = goog.ds.FastDataNode.fromJs(value, key, this);
<a name="line626"></a>    }
<a name="line627"></a>    var index = this.getKeyAsNumber_(key);
<a name="line628"></a>    if (index != null) {
<a name="line629"></a>      if (index &lt; 0 || index &gt;= this.values_.length) {
<a name="line630"></a>        throw Error(&#39;List index out of bounds: &#39; + index);
<a name="line631"></a>      }
<a name="line632"></a>      this.values_[key] = value;
<a name="line633"></a>    } else {
<a name="line634"></a>      if (!this.map_) {
<a name="line635"></a>        this.map_ = {};
<a name="line636"></a>      }
<a name="line637"></a>      this.values_.push(value);
<a name="line638"></a>      this.map_[key] = this.values_.length - 1;
<a name="line639"></a>    }
<a name="line640"></a>  } else {
<a name="line641"></a>    this.removeNode(key);
<a name="line642"></a>  }
<a name="line643"></a>  var dm = goog.ds.DataManager.getInstance();
<a name="line644"></a>  dm.fireDataChange(this.getDataPath() + goog.ds.STR_PATH_SEPARATOR + key);
<a name="line645"></a>  if (this.values_.length != count) {
<a name="line646"></a>    this.listSizeChanged_();
<a name="line647"></a>  }
<a name="line648"></a>  return null;
<a name="line649"></a>};
<a name="line650"></a>
<a name="line651"></a>
<a name="line652"></a>/**
<a name="line653"></a> * Fire data changes that are appropriate when the size of this list changes.
<a name="line654"></a> * Should be called whenever the list size has changed.
<a name="line655"></a> * @private
<a name="line656"></a> */
<a name="line657"></a>goog.ds.FastListNode.prototype.listSizeChanged_ = function() {
<a name="line658"></a>  var dm = goog.ds.DataManager.getInstance();
<a name="line659"></a>  dm.fireDataChange(this.getDataPath());
<a name="line660"></a>  dm.fireDataChange(this.getDataPath() + goog.ds.STR_PATH_SEPARATOR +
<a name="line661"></a>      &#39;count()&#39;);
<a name="line662"></a>};
<a name="line663"></a>
<a name="line664"></a>
<a name="line665"></a>/**
<a name="line666"></a> * Returns whether this data node is a list. Always returns true.
<a name="line667"></a> * @return {boolean} Whether this data node is array-like.
<a name="line668"></a> * @override
<a name="line669"></a> */
<a name="line670"></a>goog.ds.FastListNode.prototype.isList = function() {
<a name="line671"></a>  return true;
<a name="line672"></a>};
<a name="line673"></a>
<a name="line674"></a>
<a name="line675"></a>/**
<a name="line676"></a> * Returns a javascript object representation of this data node. You should
<a name="line677"></a> * not modify the object returned by this function.
<a name="line678"></a> * @return {Object} Javascript object representation of this data node.
<a name="line679"></a> */
<a name="line680"></a>goog.ds.FastListNode.prototype.getJsObject = function() {
<a name="line681"></a>  var result = [];
<a name="line682"></a>  for (var i = 0; i &lt; this.values_.length; ++i) {
<a name="line683"></a>    result.push(this.values_[i].getJsObject());
<a name="line684"></a>  }
<a name="line685"></a>  return result;
<a name="line686"></a>};
<a name="line687"></a>
<a name="line688"></a>
<a name="line689"></a>/*
<a name="line690"></a> * Implementation of goog.ds.DataNodeList for FastListNode.
<a name="line691"></a> */
<a name="line692"></a>
<a name="line693"></a>
<a name="line694"></a>/**
<a name="line695"></a> * Adds a child to this data node
<a name="line696"></a> * @param {goog.ds.DataNode} value Child node to add.
<a name="line697"></a> * @override
<a name="line698"></a> */
<a name="line699"></a>goog.ds.FastListNode.prototype.add = function(value) {
<a name="line700"></a>  if (!value.getDataName) {
<a name="line701"></a>    value = goog.ds.FastDataNode.fromJs(value,
<a name="line702"></a>        String(&#39;[&#39; + (this.values_.length) + &#39;]&#39;), this);
<a name="line703"></a>  }
<a name="line704"></a>  this.values_.push(value);
<a name="line705"></a>  var dm = goog.ds.DataManager.getInstance();
<a name="line706"></a>  dm.fireDataChange(this.getDataPath() + goog.ds.STR_PATH_SEPARATOR +
<a name="line707"></a>      &#39;[&#39; + (this.values_.length - 1) + &#39;]&#39;);
<a name="line708"></a>  this.listSizeChanged_();
<a name="line709"></a>};
<a name="line710"></a>
<a name="line711"></a>
<a name="line712"></a>/**
<a name="line713"></a> * Gets the value of this data node (if called without opt_key) or
<a name="line714"></a> * gets a child node (if called with opt_key).
<a name="line715"></a> * @param {string=} opt_key Name of child node.
<a name="line716"></a> * @return {Array|goog.ds.DataNode} Array of child nodes (if called without
<a name="line717"></a> *     opt_key), or a named child node otherwise.
<a name="line718"></a> * @override
<a name="line719"></a> */
<a name="line720"></a>goog.ds.FastListNode.prototype.get = function(opt_key) {
<a name="line721"></a>  // if there are no arguments, DataNode.get was called
<a name="line722"></a>  if (!goog.isDef(opt_key)) {
<a name="line723"></a>    return this.values_;
<a name="line724"></a>  } else {
<a name="line725"></a>    return this.getChildNode(opt_key);
<a name="line726"></a>  }
<a name="line727"></a>};
<a name="line728"></a>
<a name="line729"></a>
<a name="line730"></a>/**
<a name="line731"></a> * Gets a child node by (numeric) index.
<a name="line732"></a> * @param {number} index Index of child node (starting from 0).
<a name="line733"></a> * @return {goog.ds.DataNode} Child node at specified index.
<a name="line734"></a> * @override
<a name="line735"></a> */
<a name="line736"></a>goog.ds.FastListNode.prototype.getByIndex = function(index) {
<a name="line737"></a>  var child = this.values_[index];
<a name="line738"></a>  return (child != null ? child : null); // never return undefined
<a name="line739"></a>};
<a name="line740"></a>
<a name="line741"></a>
<a name="line742"></a>/**
<a name="line743"></a> * Gets the number of child nodes.
<a name="line744"></a> * @return {number} Number of child nodes.
<a name="line745"></a> * @override
<a name="line746"></a> */
<a name="line747"></a>goog.ds.FastListNode.prototype.getCount = function() {
<a name="line748"></a>  return this.values_.length;
<a name="line749"></a>};
<a name="line750"></a>
<a name="line751"></a>
<a name="line752"></a>/**
<a name="line753"></a> * Sets a child node.
<a name="line754"></a> * @param {string} name Name of child node.
<a name="line755"></a> * @param {Object} value Value of child node.
<a name="line756"></a> * @override
<a name="line757"></a> */
<a name="line758"></a>goog.ds.FastListNode.prototype.setNode = function(name, value) {
<a name="line759"></a>  throw Error(&#39;Setting child nodes of a FastListNode is not implemented, yet&#39;);
<a name="line760"></a>};
<a name="line761"></a>
<a name="line762"></a>
<a name="line763"></a>/**
<a name="line764"></a> * Removes a child node.
<a name="line765"></a> * @override
<a name="line766"></a> */
<a name="line767"></a>goog.ds.FastListNode.prototype.removeNode = function(name) {
<a name="line768"></a>  var index = this.getKeyAsNumber_(name);
<a name="line769"></a>  if (index == null &amp;&amp; this.map_) {
<a name="line770"></a>    index = this.map_[name];
<a name="line771"></a>  }
<a name="line772"></a>  if (index != null) {
<a name="line773"></a>    this.values_.splice(index, 1);
<a name="line774"></a>    if (this.map_) {
<a name="line775"></a>      var keyToDelete = null;
<a name="line776"></a>      for (var key in this.map_) {
<a name="line777"></a>        if (this.map_[key] == index) {
<a name="line778"></a>          keyToDelete = key;
<a name="line779"></a>        } else if (this.map_[key] &gt; index) {
<a name="line780"></a>          --this.map_[key];
<a name="line781"></a>        }
<a name="line782"></a>      }
<a name="line783"></a>      if (keyToDelete) {
<a name="line784"></a>        delete this.map_[keyToDelete];
<a name="line785"></a>      }
<a name="line786"></a>    }
<a name="line787"></a>    var dm = goog.ds.DataManager.getInstance();
<a name="line788"></a>    dm.fireDataChange(this.getDataPath() + goog.ds.STR_PATH_SEPARATOR +
<a name="line789"></a>        &#39;[&#39; + index + &#39;]&#39;);
<a name="line790"></a>    this.listSizeChanged_();
<a name="line791"></a>  }
<a name="line792"></a>  return false;
<a name="line793"></a>};
<a name="line794"></a>
<a name="line795"></a>
<a name="line796"></a>/**
<a name="line797"></a> * Returns the index of a named child nodes. This method only works if
<a name="line798"></a> * this list uses mixed name/indexed lookup, i.e. if its child node have
<a name="line799"></a> * an &#39;id&#39; attribute.
<a name="line800"></a> * @param {string} name Name of child node to determine index of.
<a name="line801"></a> * @return {number} Index of child node named name.
<a name="line802"></a> */
<a name="line803"></a>goog.ds.FastListNode.prototype.indexOf = function(name) {
<a name="line804"></a>  var index = this.getKeyAsNumber_(name);
<a name="line805"></a>  if (index == null &amp;&amp; this.map_) {
<a name="line806"></a>    index = this.map_[name];
<a name="line807"></a>  }
<a name="line808"></a>  if (index == null) {
<a name="line809"></a>    throw Error(&#39;Cannot determine index for: &#39; + name);
<a name="line810"></a>  }
<a name="line811"></a>  return /** @type {number} */(index);
<a name="line812"></a>};
</pre>


</body>
</html>
